﻿#region Using Statemens
using System;
using System.Collections.Generic;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.Graphics;
using StarFox.ScreenComponents;
#endregion

namespace StarFox.Screens
{
    /// <summary>
    /// Grahics options menu screen of the game.
    /// </summary>
    public class GraphicsOptionsScreen : StarFoxScreen
    {
        #region Fields

        SpriteSheet spriteSheet;

        // brightness sample
        Rectangle dstRectBrtSample;

        #endregion

        #region Initialization

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screenManager">The ScreenManager that this Screen is associated to.</param>
        public GraphicsOptionsScreen(ScreenManager screenManager)
            : base(screenManager)
        {
            Name = "GraphicsOptionsScreen";
        }

        /// <summary>
        /// Called when the Screen needs to be initialized.
        /// </summary>
        public override void Initialize()
        {
            // game settings management
            // game settings management...
            // ... screen brightness level (0 - 10)
            GameSettings gameSettings = Game.Services.GetService(typeof(GameSettings)) as GameSettings;
            if (gameSettings == null)
                throw new InvalidOperationException("Service of type GameSettings not found");

            DisplayProcessor displayEffect = Game.Services.GetService(typeof(DisplayProcessor)) as DisplayProcessor;
            if (displayEffect == null)
                throw new InvalidOperationException("Service of type DisplayProcessingEffect not found");

            Components3D.Add(new MenuBackground(this) { Duration = TimeSpan.FromSeconds(6) });

            ApplicationMenu menu = new ApplicationMenu(this);
            // menu title
            menu.MenuTitle = "TITLE_GRAPHICS_OPTIONS";
            // menu items
            List<ApplicationMenu.IMenuItem> auxMenuItems = new List<ApplicationMenu.IMenuItem>();
            auxMenuItems.Add(new ApplicationMenu.BarItem("ITEM_BRIGHTNESS", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.BACK, ApplicationMenu.ButtonMessage.DEFAULTS }, gameSettings.BrightnessLevel, gameSettings.DefBrightnessLevel, 10, 0));
            auxMenuItems.Add(new ApplicationMenu.BarItem("ITEM_CONTRAST", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.BACK, ApplicationMenu.ButtonMessage.DEFAULTS }, gameSettings.ContrastLevel, gameSettings.DefContrastLevel, 10, 0));
            menu.MenuItems = auxMenuItems;
            // menu actions
            menu.MenuScroll += (sender, args) =>
            {
                switch (args.Index)
                {
                    case 0:
                        // brightness
                        gameSettings.BrightnessLevel = ((ApplicationMenu.BarItem)menu.MenuItems[args.Index.Value]).CurrentBarLevel;
                        displayEffect.Brightness = gameSettings.BrightnessLevel;
                        break;
                    case 1:
                        // contrast
                        gameSettings.ContrastLevel = ((ApplicationMenu.BarItem)menu.MenuItems[args.Index.Value]).CurrentBarLevel;
                        displayEffect.Contrast = gameSettings.ContrastLevel;
                        break;
                    default:
                        break;
                }
            };
            menu.MenuDefault += (sender, args) =>
                {
                    // default values of menu items
                    gameSettings.DefaultDisplaySettings();
                    displayEffect.Brightness = gameSettings.BrightnessLevel;
                    displayEffect.Contrast = gameSettings.ContrastLevel;
                };

            Components2D.Add(menu);

            // brightness sample
            spriteSheet = Game.Content.Load<SpriteSheet>(ContentSettings.GetAsset("MENU_SPRITES"));
            Rectangle auxRect = spriteSheet.SourceRectangle("BRIGHTNESS_SAMPLE");
            dstRectBrtSample = new Rectangle((int)(Width * 0.098f), (int)(Height * 0.475f), (int)(auxRect.Width * Scale), (int)(auxRect.Height * Scale));

            base.Initialize();
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when 2D content of the Screen needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        protected override void Draw2D(GameTime gameTime)
        {
            base.Draw2D(gameTime);

            SpriteBatch.Begin();

            // brightness sample
            SpriteBatch.Draw(spriteSheet.Texture, dstRectBrtSample, spriteSheet.SourceRectangle("BRIGHTNESS_SAMPLE_FRAME"), Color.Yellow);
            SpriteBatch.Draw(spriteSheet.Texture, dstRectBrtSample, spriteSheet.SourceRectangle("BRIGHTNESS_SAMPLE"), Color.White);
            
            SpriteBatch.End();
        }

        #endregion
    }
}
